import { applyDecorators } from '@nestjs/common';
import {
  OmitType,
  PartialType as PartialTypeFormSwagger,
} from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  IsString,
  IsBoolean,
  IsNumber,
  IsEmail,
  MinLength,
  MaxLength,
  IsOptional,
  IsNotEmpty,
  Validate,
} from 'class-validator';
import {
  IsUsernameUniqueConstraint,
  // IsUsernameUnique,
} from '../../shared/validators/user-validator';
import { ApiProperty } from '@nestjs/swagger';
import { i18nValidationMessage } from 'nestjs-i18n';
import { PartialType } from '@nestjs/mapped-types';
export class CreateUserDto {
  // @startsWith('u_', { message: '用户名必须以u_开头' })
  // @IsUsernameUnique()

  // @IsUsernameUnique({ message: '用户名已存在' })
  @ApiProperty({ description: '用户名', example: 'nick' })
  @Validate(IsUsernameUniqueConstraint, [], {
    message: i18nValidationMessage('validation.usernameIsNotUnique'),
  })
  username: string;
  @EmailValidators()
  @ApiProperty({ description: '邮件', example: 'nick@qq.com' })
  email: string;
  @MobileValidators()
  @ApiProperty({ description: '手机号', example: '12345678901' })
  mobile: string;
  @PasswordValidators()
  @ApiProperty({ description: '密码', example: 'nick' })
  password: string;
  // @StatusValidators()
  // @ApiProperty({ description: '状态', example: 1 })
  status: any;
  @IsSuperValidators()
  @ApiProperty({ description: '是否是超级管理员', example: true })
  is_super: boolean;
  @SortValidators()
  @Type(() => Number)
  @ApiProperty({ description: '排序', example: 1 })
  sort: number;
}

export class UpdateUserDto extends PartialTypeFormSwagger(
  OmitType(PartialType(CreateUserDto), ['password', 'username']),
) {
  @ApiProperty({ description: '用户id', example: 1 })
  @IsOptional()
  @IsNumber()
  id: number;

  @IsString()
  @IsOptional()
  @ApiProperty({ description: '用户名', example: 'nick' })
  username: string;

  @ApiProperty({ description: '密码', example: 'nick' })
  @IsOptional()
  password: string;
}

function PasswordValidators() {
  return applyDecorators(
    IsString(),
    MinLength(6, {
      message: i18nValidationMessage('validation.minLength', {
        field: 'password',
        length: 6,
      }),
    }),
    IsNotEmpty({
      message: i18nValidationMessage('validation.isNotEmpty', {
        field: 'password',
      }),
    }),
    MaxLength(8, {
      message: i18nValidationMessage('validation.maxLength', {
        field: 'password',
        length: 8,
      }),
    }),
  );
}
function EmailValidators() {
  return applyDecorators(IsEmail(), IsOptional());
}

function MobileValidators() {
  return applyDecorators(IsString(), IsOptional());
}
function StatusValidators() {
  return applyDecorators(IsNumber(), IsOptional());
}
function IsSuperValidators() {
  return applyDecorators(IsBoolean(), IsOptional());
}

function SortValidators() {
  return applyDecorators(IsNumber(), IsOptional());
}
